import {
	BASE_URL as def_base,
	IMConfig as def_im,
	Bucket,
	Region,
	APIaddress
} from "@/utils/constant.js"
// 456  
const CONFIG_FILE =  APIaddress
// export const base_list = [def_base] 
export const base_list = [] 
// export const im_list = [def_im] 
export const im_list = [] 
var httpUrl = ''
var imUrl = {}
let inited=false;
let curIMConfig = imUrl
const init = () => {
	return new Promise((resolve,reject)=>{
		uni.request({
			url:CONFIG_FILE,
			success:({data=[]})=>{
                console.log(data,"data")	
				// uni.setStorage({
				//     key:"httpUrl",
				//     data:data[0].BASE_URL,
				//     success: function (){
				//         console.log("存储成功")
				//     }
				// })data[0].BASE_URL,
				httpUrl = data[0].BASE_URL
				imUrl=data[0].IMConfig,
				curIMConfig = imUrl;
				Array.isArray(data)&&data.forEach(({BASE_URL,IMConfig})=>{
					base_list.unshift(BASE_URL)
					im_list.unshift(IMConfig)
				})
				// im_list.push(data[0].IMConfig)
    //             base_list.push(httpUrl) //
                // im_list.push(def_im)
				console.log(base_list,im_list)
				inited=true
				resolve()
			},
			fail: () => {
				inited=false
				resolve()
			}
		})
	})
}

export const initTask = init() 

const getNetworkStatus=()=>{
	
	uni.getSystemInfo({
		success:res=>{
			uni.onNetworkStatusChange(function(ret){
				if(!uni.getStorageSync("CommonNetConnected")&&res.platform=='ios'){
					uni.setStorageSync("CommonNetConnected",true)
					setTimeout(()=>{
						plus.runtime.restart()
						if(!inited){
							init()
						}
					},1000)
				}else{
					uni.setStorageSync("CommonNetConnected",true)
					if(!inited){
						init()
					}
				}
			})
		}
	})
}




getNetworkStatus()

let curBaseUrl = def_base

export const getBaseUrl = () => {
	return  httpUrl
	// curBaseUrl
}

export const setBaseUrl = (baseUrl) => {
	curBaseUrl = baseUrl
	httpUrl = baseUrl
	console.log(baseUrl,"setBaseUrl")
}
export const setNextBaseUrl = () => {
	curBaseUrl = base_list[1]
	httpUrl = base_list[1]
	console.log(baseUrl,"setBaseUrl")
}

export const getIMConfig = () => {
	return  curIMConfig
	// curIMConfig
}

export const setIMConfig = (IMConfig) => {
	curIMConfig = IMConfig
	console.log(IMConfig,"setIMConfig")
}